package com.compass.doctor.ui.setting

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.activity.viewModels
import com.compass.doctor.R
import com.compass.doctor.callback.OnCustomButtonClick
import com.compass.doctor.databinding.ActivityConsultSettingBinding
import com.compass.doctor.dialog.SetDataDialog
import com.compass.doctor.ui.setting.viewmodel.ConsultSettingViewModel
import com.compass.doctor.utils.StringUtils
import com.compass.doctor.base.BaseVBActivity
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.ext.gone
import com.compass.framework.ext.onClick
import com.compass.framework.ext.visible
import com.compass.framework.manager.UserInfoManager
import com.compass.framework.model.ConsultSetting
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getStringFromResource
import com.loper7.date_time_picker.DateTimeConfig
import com.loper7.date_time_picker.dialog.CardDatePickerDialog
import com.loper7.date_time_picker.dialog.CardWeekPickerDialog
import java.util.Arrays

/**
 * Description： 咨询设置
 * Date:2023/12/18 15:40
 */
class ConsultSettingActivity : BaseVBActivity<ActivityConsultSettingBinding>() {


    companion object {
        fun start(context: Context) {
            val intent = Intent(context, ConsultSettingActivity::class.java)
            context.startActivity(intent)
        }
    }

    private var consultSetting: ConsultSetting? = null
    private val consultSettingViewModel: ConsultSettingViewModel by viewModels()
    private var consultationIsOpen: Boolean = true
    private var textIsOpen: Boolean = true
    private var phoneIsOpen: Boolean = true
    private var videoIsOpen: Boolean = true
    private var requestType: Int = 1
    private var startDate: Long = 0
    private var textDate: ArrayList<String> = ArrayList()
    private var phoneDate: ArrayList<String> = ArrayList()
    private var videoDate: ArrayList<String> = ArrayList()

    //日期弹窗回调
    private var onChatBottomClick: OnCustomButtonClick = object : OnCustomButtonClick {
        override fun onButtonCallBack(comeType: Int, date: String?) {
            if (!date.isNullOrEmpty()) {
                setDate(date.substring(1, date.length - 1), comeType)
            }
        }
    }

    //图文回调
    private val mActivityResultLauncherText: ActivityResultLauncher<Intent>? =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            if (it.resultCode == RESULT_OK) {
                it.data?.let { data ->
                    val price = data.getDoubleExtra("price", 0.0)
                    when (requestType) {
                        1 -> {
                            setPrice(price, 1)

                        }

                        2 -> {
                            setPrice(price, 2)

                        }

                        3 -> {

                            setPrice(price, 3)
                        }
                    }
                }
            }
        }

    override fun initView(savedInstanceState: Bundle?) {
        //如果是医生
//        if (UserInfoManager.getUserType() == "1"){
//            mBinding.clyPhone.visible()
//            mBinding.clyVideo.visible()
//        }else{
//            mBinding.clyPhone.gone()
//            mBinding.clyVideo.gone()
//        }
        mBinding.imgClose.onClick {
            mBinding.llSettingTips.visibility = View.GONE
        }
        mBinding.consultationSc.onClick {
            consultationIsOpen = !consultationIsOpen
            mBinding.consultationSc.isChecked = consultationIsOpen
            consultSetting?.consultationStatus = if (consultationIsOpen) 1 else 0
        }
        mBinding.textSc.onClick {
            textIsOpen = !textIsOpen
            consultSetting?.txtStatus = if (textIsOpen) 1 else 0
            mBinding.textSc.isChecked = textIsOpen
            setClinicalText(textIsOpen)
        }
        mBinding.phoneSc.onClick {
            phoneIsOpen = !phoneIsOpen
            consultSetting?.phoneStatus = if (phoneIsOpen) 1 else 0
            mBinding.phoneSc.isChecked = phoneIsOpen
            setClinicalPhone(phoneIsOpen)
        }
        mBinding.videoSc.onClick {
            videoIsOpen = !videoIsOpen
            consultSetting?.videoStatus = if (videoIsOpen) 1 else 0
            mBinding.videoSc.isChecked = videoIsOpen
            setClinicalVideo(videoIsOpen)
        }

        mBinding.llTxtExplainPriceSingle.onClick {
            requestType = 1
            showToast("图文问诊不支持修改费用")
        }
        mBinding.llPhoneSingle.onClick {

            if (UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.isSelfUpDateFee=="1"){
                requestType = 2
                val intent = Intent(this, ExpensesSettingActivity::class.java)
                intent.putExtra(KEY_CONSULT_TYPE, requestType)
                mActivityResultLauncherText?.launch(intent)
            }else{
                showToast("暂不支持修改费用")
            }


        }


        mBinding.llVideoSingle.onClick {

            if (UserInfoManager.getUserInfo()?.appHospitalConfigRespVO?.isSelfUpDateFee=="1"){
                requestType = 3
                val intent = Intent(this, ExpensesSettingActivity::class.java)
                intent.putExtra(KEY_CONSULT_TYPE, requestType)
                mActivityResultLauncherText?.launch(intent)
            }else{
                showToast("暂不支持修改费用")
            }

        }


        mBinding.txtDate.onClick {
            requestType = 1
            SetDataDialog.Builder(this, requestType, textDate, onChatBottomClick).show()
        }
        mBinding.phoneDate.onClick {
            requestType = 2
            SetDataDialog.Builder(this, requestType, phoneDate, onChatBottomClick).show()
        }
        mBinding.videoDate.onClick {
            requestType = 3
            SetDataDialog.Builder(this, requestType, videoDate, onChatBottomClick).show()
        }

        mBinding.txtTime.onClick {
            showDateDialog(1, false)
        }
        mBinding.phoneTime.onClick {
            showDateDialog(2, false)
        }
        mBinding.videoTime.onClick {
            showDateDialog(3, false)
        }

        mBinding.tvSave.onClick {
            consultSetting?.let {


                if (it.txtStatus == 1 && it.consultationStatus == 1) {

                    if (it.txtDate.isNullOrEmpty()) {

                        showToast("请设置图文接诊日期")
                        return@let
                    }

                    if (it.txtTime.isNullOrEmpty()) {

                        showToast("请设置图文接诊时间")
                        return@let
                    }

                }
                if (it.phoneStatus == 1 && it.consultationStatus == 1) {

                    if (it.phoneDate.isNullOrEmpty()) {

                        showToast("请设置电话接诊日期")
                        return@let
                    }

                    if (it.phoneTime.isNullOrEmpty()) {

                        showToast("请设置电话接诊时间")
                        return@let
                    }

                }

                if (it.videoStatus == 1 && it.consultationStatus == 1) {

                    if (it.videoDate.isNullOrEmpty()) {

                        showToast("请设置视频接诊日期")
                        return@let
                    }

                    if (it.videoTime.isNullOrEmpty()) {

                        showToast("请设置视频接诊时间")
                        return@let
                    }

                }



                consultSettingViewModel.saveConsultationConfig(consultSetting)
            }

        }
    }

    override fun initData() {
        consultSettingViewModel.getConsultationConfig().observe(this) {
            it?.let {
                consultSetting = it
                setConsultSettingData()

            }
        }
        consultSettingViewModel.saveConsultLiveData.observe(this) {
            it?.let { successfully ->
                if (successfully) {
                    showToast(R.string.modified_successfully)
                    finish()
                } else {
                    showToast(R.string.modification_failure)
                }

            }
        }

    }


    //设置咨询数据
    private fun setConsultSettingData() {
        consultSetting?.let {
            consultationIsOpen = it.consultationStatus == 1
            mBinding.consultationSc.isChecked = consultationIsOpen
            textIsOpen = it.txtStatus == 1
            phoneIsOpen = it.phoneStatus == 1
            videoIsOpen = it.videoStatus == 1
            setClinicalText(textIsOpen)
            setClinicalPhone(phoneIsOpen)
            setClinicalVideo(videoIsOpen)
            setPrice(it.txtPrice, 1)
            setPrice(it.phonePrice, 2)
            setPrice(it.videoPrice, 3)
            setDate(it.txtDate, 1)
            setDate(it.phoneDate, 2)
            setDate(it.videoDate, 3)
            setTime(it.txtTime, 1)
            setTime(it.phoneTime, 2)
            setTime(it.videoTime, 3)
        }

    }


    private fun showDateDialog(intType: Int, isEnd: Boolean) {

        CardDatePickerDialog.builder(this)
            .setTitle(if (!isEnd) "开始时间" else "结束时间")
            .showBackNow(false)
            .setLabelText(hour = "时", min = "分")
            .setBackGroundModel(CardDatePickerDialog.STACK)

            .setMinTime(if (!isEnd) 0 else startDate)
            .setDisplayType(DateTimeConfig.HOUR, DateTimeConfig.MIN)
            .setThemeColor(getColorFromResource(R.color.color_0165b8))
            .setOnChoose { millisecond ->
                if (isEnd) {
                    var times = StringUtils.conversionTime(
                        startDate,
                        "HH:mm"
                    ) + "-" + StringUtils.conversionTime(millisecond, "HH:mm")
                    setTime(times, intType)
                } else {
                    startDate = millisecond
                    showDateDialog(intType, true)
                }

            }.build().show()
    }


    //设置图文接诊状态
    private fun setClinicalText(isOpen: Boolean) {
        mBinding.textSc.isChecked = isOpen
        mBinding.textLine.visibility = if (isOpen) View.VISIBLE else View.GONE
//        mBinding.llTxtExplainPriceSingle.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llAnnualFeeDate.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llAnnualFeeTime.visibility = if (isOpen) View.VISIBLE else View.GONE

    }

    private fun setPrice(doublePrice: Double, priceType: Int) {

        var string = if (doublePrice > 0) String.format(
            getStringFromResource(R.string.splice_symbol),
            doublePrice
        ) else getStringFromResource(R.string.price_free)
        when (priceType) {
            1 -> {
                consultSetting?.txtPrice = doublePrice
                mBinding.txtPrice.text = string
            }

            2 -> {
                consultSetting?.phonePrice = doublePrice
                mBinding.phonePrice.text = string
            }

            3 -> {
                consultSetting?.videoPrice = doublePrice
                mBinding.videoPrice.text = string

            }
        }

    }

    private fun setDate(date: String?, priceType: Int) {


        when (priceType) {
            1 -> {
                consultSetting?.txtDate = date


                if (date.isNullOrEmpty()) {
                    mBinding.txtDate.text = getStringFromResource(R.string.consult_time_tips)
                    textDate.clear()
                } else {
                    mBinding.txtDate.text = date
                    textDate = date.split(",").toList() as ArrayList
                }

            }

            2 -> {
                consultSetting?.phoneDate = date

                if (date.isNullOrEmpty()) {
                    mBinding.phoneDate.text = getStringFromResource(R.string.consult_time_tips)
                    phoneDate.clear()
                } else {
                    mBinding.phoneDate.text = date
                    phoneDate = date.split(",").toList() as ArrayList
                }
            }

            3 -> {
                consultSetting?.videoDate = date


                if (date.isNullOrEmpty()) {
                    mBinding.videoDate.text = getStringFromResource(R.string.consult_time_tips)
                    videoDate.clear()
                } else {
                    mBinding.videoDate.text = date

                    videoDate = date.split(",").toList() as ArrayList
                }
            }
        }

    }


    private fun setTime(time: String?, priceType: Int) {

        when (priceType) {
            1 -> {
                consultSetting?.txtTime = time
                time?.let {
                    mBinding.txtTime.text = time

                } ?: {
                    mBinding.txtTime.text = getStringFromResource(R.string.consult_date_tips)
                }


            }

            2 -> {
                consultSetting?.phoneTime = time
                time?.let {
                    mBinding.phoneTime.text = time

                } ?: {
                    mBinding.phoneTime.text = getStringFromResource(R.string.consult_date_tips)
                }

            }

            3 -> {
                consultSetting?.videoTime = time
                time?.let {
                    mBinding.videoTime.text = time

                } ?: {
                    mBinding.videoTime.text = getStringFromResource(R.string.consult_date_tips)
                }
            }
        }

    }


    //设置图文接诊状态
    private fun setClinicalPhone(isOpen: Boolean) {
        mBinding.phoneSc.isChecked = isOpen
        mBinding.phoneLine.visibility = if (isOpen) View.VISIBLE else View.GONE
//        mBinding.llPhoneSingle.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llPhoneFeeDate.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llPhoneFeeTime.visibility = if (isOpen) View.VISIBLE else View.GONE

    }

    //设置图文接诊状态
    private fun setClinicalVideo(isOpen: Boolean) {
        mBinding.videoSc.isChecked = isOpen
        mBinding.videoLine.visibility = if (isOpen) View.VISIBLE else View.GONE
//        mBinding.llVideoSingle.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llVideoFeeDate.visibility = if (isOpen) View.VISIBLE else View.GONE
        mBinding.llVideoFeeTime.visibility = if (isOpen) View.VISIBLE else View.GONE


    }


}